﻿using Microsoft.Practices.EnterpriseLibrary.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QLCHBANGIAY.DATA
{
    public class custommerController
    {
        Database db = DatabaseFactory.CreateDatabase();
        #region[tbCUSTOMERS_GetById]
        public List<Custommer> tbCUSTOMERS_GetById(string Id)
        {
            List<Custommer> list = new List<Custommer>();
            Custommer obj = new Custommer();
            DbCommand cmd = db.GetStoredProcCommand("sp_tbCUSTOMERS_GetById", Id);
            using (IDataReader dr = db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    list.Add(obj.customerIDataReader(dr));
                }
                dr.Close();
                dr.Dispose();
            }
            return list;
        }
        #endregion
        #region[tbCUSTOMERS_Get_Customer_Order]

        public DataSet Get_Customer_Order_By_lang(string Lang)
        {
            DataTable dt = new DataTable();
            DbCommand cmd = db.GetStoredProcCommand("sp_Get_Customer_Order_By_lang", Lang);
            return db.ExecuteDataSet(cmd);

        }
        #endregion
        #region[tbCUSTOMERS_GetByTop]
        public List<Custommer> tbCUSTOMERS_GetByTop(string Top, string Where, string Order)
        {
            List<Custommer> list = new List<Custommer>();
            Custommer obj = new Custommer();
            DbCommand cmd = db.GetStoredProcCommand("sp_tbCUSTOMERS_GetByTop", Top, Where, Order);
            using (IDataReader dr = db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    list.Add(obj.customerIDataReader(dr));
                }
                dr.Close();
                dr.Dispose();
            }
            return list;
        }
        #endregion
        #region[tbCUSTOMERS_GetByAll]
        public List<Custommer> tbCUSTOMERS_GetByAll()
        {
            List<Custommer> list = new List<Custommer>();
            Custommer obj = new Custommer();
            DbCommand cmd = db.GetStoredProcCommand("sp_tbCUSTOMERS_GetByAll");
            using (IDataReader dr = db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    list.Add(obj.customerIDataReader(dr));
                }
                dr.Close();
                dr.Dispose();
            }
            return list;
        }
        #endregion
        #region[tbCUSTOMERS_Paging]
        public List<Custommer> tbCUSTOMERS_Paging(string CurentPage, string PageSize)
        {
            List<Custommer> list = new List<Custommer>();
            Custommer obj = new Custommer();
            using (DbCommand cmd = db.GetStoredProcCommand("sp_tbCUSTOMERS_Paging", CurentPage, PageSize))
            {
                try
                {
                    using (IDataReader dr = db.ExecuteReader(cmd))
                    {
                        while (dr.Read())
                        {
                            list.Add(obj.customerIDataReader(dr));
                        }
                        dr.Close();
                        dr.Dispose();
                    }
                }
                catch (Exception ex)
                {
                    //throw ex;
                }
                finally
                {
                    if (cmd != null) cmd.Dispose();
                }
            }
            return list;
        }
        #endregion
        #region[tbCUSTOMERS_Insert]
        public bool tbCUSTOMERS_Insert(Custommer data)
        {
            using (DbCommand cmd = db.GetStoredProcCommand("sp_tbCUSTOMERS_Insert"))
            {
                cmd.Parameters.Add(new SqlParameter("@vusername", data.vusername));
                cmd.Parameters.Add(new SqlParameter("@vpassword", data.vpassword));
                cmd.Parameters.Add(new SqlParameter("@vcusname", data.vcusname));
                cmd.Parameters.Add(new SqlParameter("@dbirthday", data.dbirthday));
                cmd.Parameters.Add(new SqlParameter("@vprovince", data.vprovince));
                cmd.Parameters.Add(new SqlParameter("@vaddress", data.vaddress));
                cmd.Parameters.Add(new SqlParameter("@vphone", data.vphone));
                cmd.Parameters.Add(new SqlParameter("@vmobile", data.vmobile));
                cmd.Parameters.Add(new SqlParameter("@vemail", data.vemail));
                cmd.Parameters.Add(new SqlParameter("@dcreatedate", data.dcreatedate));
                cmd.Parameters.Add(new SqlParameter("@istatus", data.istatus));
                try
                {
                    db.ExecuteNonQuery(cmd);
                    return true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (cmd != null) cmd.Dispose();
                }
            }
        }
        #endregion
        #region[tbCUSTOMERS_Update]
        public bool tbCUSTOMERS_Update(Custommer data)
        {
            using (DbCommand cmd = db.GetStoredProcCommand("sp_tbCUSTOMERS_Update"))
            {
                cmd.Parameters.Add(new SqlParameter("@iusid", data.iusid));
                cmd.Parameters.Add(new SqlParameter("@vusername", data.vusername));
                cmd.Parameters.Add(new SqlParameter("@vpassword", data.vpassword));
                cmd.Parameters.Add(new SqlParameter("@vcusname", data.vcusname));
                cmd.Parameters.Add(new SqlParameter("@dbirthday", data.dbirthday));
                cmd.Parameters.Add(new SqlParameter("@vprovince", data.vprovince));
                cmd.Parameters.Add(new SqlParameter("@vaddress", data.vaddress));
                cmd.Parameters.Add(new SqlParameter("@vphone", data.vphone));
                cmd.Parameters.Add(new SqlParameter("@vmobile", data.vmobile));
                cmd.Parameters.Add(new SqlParameter("@vemail", data.vemail));
                cmd.Parameters.Add(new SqlParameter("@dcreatedate", data.dcreatedate));
                cmd.Parameters.Add(new SqlParameter("@istatus", data.istatus));
                try
                {
                    db.ExecuteNonQuery(cmd);
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
                finally
                {
                    if (cmd != null) cmd.Dispose();
                }
            }
        }
        #endregion
        #region[tbCUSTOMERS_Delete]
        public bool tbCUSTOMERS_Delete(string Id)
        {
            DbCommand cmd = db.GetStoredProcCommand("sp_tbCUSTOMERS_Delete", Id);
            try
            {
                db.ExecuteNonQuery(cmd);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                if (cmd != null) cmd.Dispose();
            }
        }
        #endregion
    }
}
